package pers.qianyu.month_202012.date_20201219.difference;

/**
 * 1109. 航班预订统计
 * https://leetcode-cn.com/problems/corporate-flight-bookings/
 *
 * @author mizzle rain
 * @date 2020-12-19 16:29
 */
public class CorpFlightBookings {
    // 差分数组例题
    public int[] corpFlightBookings(int[][] bookings, int n) {
        int[] diff = new int[n];
        for (int[] booking : bookings) {
            diff[booking[0] - 1] += booking[2];
            if (booking[1] < n) {
                diff[booking[1]] -= booking[2];
            }
        }
        int[] res = new int[n];
        res[0] = diff[0];
        for (int i = 1; i < diff.length; i++) {
            res[i] = res[i - 1] + diff[i];
        }
        return res;
    }
}
